﻿<cfscript>
/** 
* @hint "bookBase - Book, BookSchema, BookPublisher"
*/ 

component 
	extends="MachII.framework.Listener" 
	displayname="acadmicManager.listeners.senate.bookListener" 
	output=false
	depends="sequenceBookPublisher, sequenceBook"
	{
	
	/**
	* @hint "Configures this listener as part of the Mach-II framework"
	*/
	public void function configure() output=false {} 
	
	/**
	* @hint "删除教材信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function deleteBook( required MachII.framework.Event event ) output=false {
	
		var book = "";
		var bookId = event.getArg("BookID");
		var sql = queryObj = "";
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "	DELETE FROM 
						t_book b 
					WHERE 
						b.bid = :bookId ";

			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "bookId", value = bookId, cfsqltype = "cf_sql_varchar");
			queryObj.execute(sql = sql);

		}
		
	}
	
	/**
	* @hint "更新教材信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function updateBook( required MachII.framework.Event event ) output=false {
		
		var book = "";
		var pub = "";
		var passArg = structNew();
		
		var sql = queryObj = "";
		
		var auther = [];
		var pubId = event.getArg("PubID");
		var bookId = event.getArg("BookID");
		
		var bookName = trim(event.getArg("BookName"));
		var authors = trim(event.getArg("Authors"));
		var edition = event.getArg("Edition1") & "." & event.getArg("Edition2");
		var edition2 = event.getArg("Edition");
		var isbn = trim( replace(event.getArg("ISBN"), "-", "", "ALL") );
		
		var py = trim(event.getArg("PinYin"));
		var char = "";
		var checkQuery = [];
		var i = 1;
		
		var t = "";

		/* 处理版次问题 */
		if ( len(edition2) ) {
			edition = edition2;
		}
		
		/* 处理作者分割问题 */
		auther = listToArray(authors, ",");
		authors = "";
		
		for ( t in auther ) {
			authors = listAppend(authors, trim(t), "/");
		}
		
		/* 生成拼音简写 */
		if ( len(py) eq 0 ) {
			for ( i=1; i lte len(bookName); i++) {
			
				char = mid(bookName, i, 1);
			
				if ( refind("^[A-Za-z0-9]+$", char) ) {
					py = py & char;
				}
				else {
			
					/* 索检拼音 */
					sql = "	SELECT
								c.charidx
							FROM
								t_chinese c
							WHERE
								c.character = :char ";
					
					queryObj = new Query(datasource = application.dnsSlave);
					queryObj.addParam(name = "char", value = char, cfsqltype = "cf_sql_char");
					rs_checkQuery = queryObj.execute(sql = sql).getResult();

					if (rs_checkQuery.recordCount) {
						py = py & rs_checkQuery.charidx;
					}

				}

			}
		}
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "	SELECT
						p.pid
					FROM
						t_publisher p
					WHERE
						p.pid = :pubId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "pubId", value = pubId, cfsqltype = "cf_sql_char");
			rs_pubEntity = queryObj.execute(sql = sql).getResult();
			
			sql = "	SELECT
						b.bid
					FROM
						t_book b
					WHERE
						b.bid = :bookId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "bookId", value = bookId, cfsqltype = "cf_sql_varchar");
			rs_bookEntity = queryObj.execute(sql = sql).getResult();
			
			if (rs_pubEntity.recordCount and rs_bookEntity.recordCount ) {
				
				/* 处理ISBN为空 */
				if ( isbn eq "UUID" ) {
					isbn = bookId;
				}
				
				sql = "	UPDATE
							t_book b
						SET
							b.pid = :pubId,
							b.book_name = :bookName,
							b.book_authors = :bookAuthors,
							b.book_edition = :bookEdition,
							b.book_isbn = :bookISBN,
							b.book_awards = :bookAwards,
							b.book_prise = :bookPrise,
							b.pyidx = :pyIndex,
							b.pyid = :pyFirst 
						WHERE
							b.bid = :bookId ";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "bookId", value = rs_bookEntity.bid, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "pubId", value = rs_pubEntity.pid, cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "bookName", value = bookName, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "bookAuthors", value = authors, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "bookEdition", value = edition, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "bookISBN", value = isbn, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "bookAwards", value = trim(event.getArg("Awards")), cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "bookPrise", value = event.getArg("Prise"), cfsqltype = "cf_sql_decimal", scale = 2);
				queryObj.addParam(name = "pyIndex", value = py, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "pyFirst", value = left(py,1), cfsqltype = "cf_sql_char");
				queryObj.execute(sql = sql);

				event.setArg("PubID", pubId);
			}
			
		}
		
	}
	
	/**
	* @hint "创建教材信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function createBook( required MachII.framework.Event event ) output=false {
		
		var sql = queryObj = "";
		
		var book = "";
		var pub = "";
		var passArg = structNew();
		
		var auther = [];
		var pubId = event.getArg("PubID");
		var bookId = "";
		
		var bookName = trim(event.getArg("BookName"));
		var authors = trim(event.getArg("Authors"));
		var edition = event.getArg("Edition1") & "." & event.getArg("Edition2");
		var edition2 = event.getArg("Edition");
		var isbn = trim( replace(event.getArg("ISBN"), "-", "", "ALL") );
		
		var sequenceBook = getsequenceBook();
		
		var py = "";
		var char = "";
		var checkQuery = [];
		var i = 1;
		
		/* 处理版次问题 */
		if ( len(edition2) ) {
			edition = edition2;
		}
		
		/* 规范书名 */
		bookName = replace(bookName, "（", "(", "ALL");
		bookName = replace(bookName, "）", ")", "ALL");
		bookName = replace(bookName, "《", "", "ALL");
		bookName = replace(bookName, "》", "", "ALL");
		
		/* 处理作者分割问题 */
		auther = listToArray(authors, "、，, ");
		authors = arrayToList(auther, "/");
		
		/* 生成拼音简写 */
		for ( i=1; i lte len(bookName); i++) {
			
			char = mid(bookName, i, 1);
			
			if ( refind("^[A-Za-z0-9]+$", char) ) {
				py = py & char;
			}
			else {
			
				/* 索检拼音 */
				sql = "	SELECT
							c.charidx
						FROM
							t_chinese c
						WHERE
							c.character = :char ";
				
				queryObj = new Query(datasource = application.dnsSlave);
				queryObj.addParam(name = "char", value = char, cfsqltype = "cf_sql_char");
				rs_checkQuery = queryObj.execute(sql = sql).getResult();
			
				if (rs_checkQuery.recordCount) {
					py = py & rs_checkQuery.charidx;
				}
			}

		}
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "	SELECT
						p.pid,p.book_count
					FROM
						t_publisher p
					WHERE
						p.pid = :pubId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "pubId", value = pubId, cfsqltype = "cf_sql_char");
			rs_pubEntity = queryObj.execute(sql = sql).getResult();


			if( rs_pubEntity.recordCount ){

				var bookId = sequenceBook.getID();
				
				/* 处理ISBN为空 */
				if ( isbn eq "UUID" ) {
					isbn = bookId;
				}

				sql = "	INSERT INTO t_book(
							bid,
							pid,
							book_name,
							book_authors,
							book_edition,
							book_isbn,
							book_awards,
							book_prise,
							book_stock,
							pyidx,
							pyid
						)VALUES(
							:bookId,
							:pubId,
							:bookName,
							:bookAuthors,
							:bookEdition,
							:bookISBN,
							:bookAwards,
							:bookPrise,
							:bookStock,
							:pyIndex,
							:pyFirst 
						)";

				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "bookId", value = bookId, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "pubId", value = pubId, cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "bookName", value = bookName, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "bookAuthors", value = authors, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "bookEdition", value = edition, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "bookISBN", value = isbn, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "bookAwards", value = trim(event.getArg("Awards")), cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "bookPrise", value = event.getArg("Prise"), cfsqltype = "cf_sql_decimal", scale = 2);
				queryObj.addParam(name = "bookStock", value = 0, cfsqltype = "cf_sql_integer");
				queryObj.addParam(name = "pyIndex", value = py, cfsqltype = "cf_sql_varchar");
				queryObj.addParam(name = "pyFirst", value = left(py,1), cfsqltype = "cf_sql_char");
				queryObj.execute(sql = sql);
			
				sql = "	UPDATE
							t_publisher p
						SET
							p.book_count = p.book_count + 1
						WHERE
							p.pid = :pubId ";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "pubId", value = pubId, cfsqltype = "cf_sql_char");
				queryObj.execute( sql = sql );
				
				event.setArg("PubID", pubId);

			}
		}
		
	}
	
	/**
	* @hint "合并出版社信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function comboPublisher( required MachII.framework.Event event ) output=false {
		
		var pub = "";
		var pubOld = "";
		var book = "";
		var bookIds = [];
		var bookId = "";
		
		var pubId = event.getArg("oldPubID");
		var pubIdNew = event.getArg("newPubID");

		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			sql = "	SELECT
						p.pid
					FROM
						t_publisher p
					WHERE
						p.pid = :pubId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "pubId", value = pubIdNew, cfsqltype = "cf_sql_char");
			rs_pubEntity = queryObj.execute(sql = sql).getResult();
			
			if ( rs_pubEntity.recordCount ) {
				
				sql = "	SELECT
							COUNT(b.bid) b_count
						FROM
							t_book b
						WHERE
							b.pid = :pubId ";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "pubId", value = pubId, cfsqltype = "cf_sql_char");
				rs_bookCount = queryObj.execute(sql = sql).getResult();

				sql = "	UPDATE
							t_book b
						SET
							b.pid = :pubIdNew 
						WHERE
							b.pid = :pubId ";

				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "pubId", value = pubId, cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "pubIdNew", value = pubIdNew, cfsqltype = "cf_sql_char");
				queryObj.execute(sql = sql);

				sql = "	UPDATE
							t_publisher p
						SET
							p.book_count = p.book_count + :bookCount 
						WHERE
							p.pid = :pubId ";

				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "pubId", value = pubIdNew, cfsqltype = "cf_sql_char");
				queryObj.addParam(name = "bookCount", value = rs_bookCount.b_count, cfsqltype = "cf_sql_integer");
				queryObj.execute(sql = sql);

				sql = "	DELETE FROM
							t_publisher p
						WHERE
							p.pid = :pubId ";
				
				queryObj = new Query(datasource = application.dnsMaster);
				queryObj.addParam(name = "pubId", value = pubId, cfsqltype = "cf_sql_char");
				queryObj.execute(sql = sql);
				
				event.setArg("PubID", pubIdNew);
				
			}
		}
		
	}
	
	/**
	* @hint "删除出版社信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function deletePublisher( required MachII.framework.Event event ) output=false {
		var pubId = event.getArg("pubId");
		var sql = queryObj = "";
		/* Every SQL operation is TRANSACATION*/
		transaction {
			sql = "DELETE FROM t_publisher p WHERE p.pid = :pubId ";
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "pubId", value = pubId, cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
		}
	}
	
	/**
	* @hint "创建出版社信息"
	* 
	* @event "MachII Event Object"
	*/
	public void function createPublisher( required MachII.framework.Event event ) output=false {

		var pub = "";
		var pubId = "";
		var passArg = structNew();
		var sequenceBookPublisher = getsequenceBookPublisher();
		
		var pubName = trim(event.getArg("Publisher"));

		var py = "";
		var char = "";
		var checkQuery = [];
		var i = 1;
				
		/* 生成拼音简写 */
		for ( i=1; i lte len(pubName); i++) {
			
			char = mid(pubName, i, 1);
			
			if ( refind("^[A-Za-z0-9]+$", char) ) {
				py = py & char;
			} else {
			
				/* 索检拼音 */
				sql = "	SELECT
							c.charidx
						FROM
							t_chinese c
						WHERE
							c.character = :char ";
				
				queryObj = new Query(datasource = application.dnsSlave);
				queryObj.addParam(name = "char", value = char, cfsqltype = "cf_sql_char");
				rs_checkQuery = queryObj.execute(sql = sql).getResult();
			
				if (rs_checkQuery.recordCount) {
					py = py & rs_checkQuery.charidx;
				}
			}

		}

		/* Every SQL operation is TRANSACATION*/
		transaction {

			/* PubID is AUTO-GEN when insert */
			pubId = sequenceBookPublisher.getID();
			sql = "	INSERT INTO t_publisher(
						pid,
						publisher,
						book_count,
						pyidx,
						pyid
					)VALUES(
						:pubId,
						:publisher,
						:bookCount,
						:pyIndex,
						:pyFirst 
					)";
			
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "pubId", value = pubId, cfsqltype = "cf_sql_char");
			queryObj.addParam(name = "publisher", value = pubName, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "bookCount", value = 0, cfsqltype = "cf_sql_integer");
			queryObj.addParam(name = "pyIndex", value = py, cfsqltype = "cf_sql_varchar");
			queryObj.addParam(name = "pyFirst", value = left(py,1), cfsqltype = "cf_sql_char");
			queryObj.execute(sql = sql);
			
			event.setArg("PubID", pubId);
			
		}
	}
	
}

</cfscript>